package game.module.qzmj.processor;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.hall.logic.QipaiDataManager;
import game.module.qzmj.bean.QzmjRoom;
import game.module.qzmj.logic.QzmjDataManager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageQzmj.PushQzmjForceDismissRoom;
import lua.LuaMessageQzmj.S2CQzmjForceDismisroom;
import lua.LuaMessageQzmj.C2SQzmjForceDismisroom;

@MsgCodeAnn(msgcode = C2SQzmjForceDismisroom.id, accessLimit = 500)
public class QzmjDismissRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(QzmjDismissRoomProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw requestMessage) throws Exception {
		// TODO Auto-generated method stub
		int playerId = hero.getId();
		logger.info("qzMj dismiss room,playeyId={}", playerId);
		boolean isInRoom = QzmjDataManager.getInstance().isPlayerInRoom(playerId);
		if (!isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		QzmjRoom mjRoom = QzmjDataManager.getInstance().getMjRoomByPlayerId(playerId);
		if (mjRoom == null) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		S2CQzmjForceDismisroom msg = new S2CQzmjForceDismisroom();
		hero.writeAndFlush(msg.build(hero.alloc()));
		List<Integer> playerIdList = mjRoom.getPlayerList();
		PushQzmjForceDismissRoom pushMsg = new PushQzmjForceDismissRoom();
		pushMsg.player_index = (byte) playerIdList.indexOf(playerId);
		if (mjRoom.getPlayerList().get(0) != playerId) {
			byte ready[] = mjRoom.getReady();
			int i =playerIdList.indexOf(playerId);
			while(i<ready.length-1) {
				ready[i] = ready[i+1];
				i=i+1;
			}
			ready[ready.length-1] = 0;
		//	ready[playerIdList.indexOf(playerId)] = 0;//清空准备状态
			Integer play = new Integer(playerId);
			for (Integer aPlayerId : playerIdList) {
				PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
				if (pr != null && pr.isChannelActive()) {
					pr.writeAndFlush(pushMsg.build(pr.alloc()));
				}
			}
			Map<Integer,String> ipmap = mjRoom.getIpmaps();
			ipmap.remove(hero.getId());
			playerIdList.remove(play); // 移除玩家
			QzmjDataManager.getInstance().removePlayer(playerId);
			logger.info("playerlist={}",playerIdList);
			return; // 移除退出房间的玩家
		}
		for (Integer aPlayerId : playerIdList) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
		QzmjDataManager.getInstance().removeRoom(mjRoom.getUuid());
		QipaiDataManager.getInstance().removeRoomUuid(mjRoom.getUuid());

	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
